# -*- coding: utf-8 -*-
'''
Created on 2013-4-9
标签  http://lishiguang.iteye.com/blog/1332529
@author: dyq
'''
from django.shortcuts import render_to_response
from django.http import HttpResponse
import json
from django.http import HttpResponseRedirect
from Cms.plus import user
from django.core.paginator import PageNotAnInteger, Paginator, InvalidPage, EmptyPage

#返回书记辅助类
class Find:
    
    #静态方法 自动查找视图
    @staticmethod 
    def View(request,obj=None):
        '''
                     自动查找页面，传入 obj 和用户对象
        '''
        #获取用户对象
        userModel=user.user().GetUser(request)
        webobj={"User":userModel,'Model':obj}
        
        return render_to_response(Find().GetTemplates(request),webobj)
    
    #静态方法 跳转到指定视图
    @staticmethod 
    def ViewUrl(url,obj):
        return render_to_response(url,obj)
    
    #静态方法  重定向
    @staticmethod 
    def Redirect(url):
        return HttpResponseRedirect(url)
    
    #静态方法 返回json格式
    @staticmethod 
    def Json(obj):
        data = json.dumps(obj)
        return HttpResponse(data, mimetype='application/json')
    
    #静态方法 返回标准异常消息的json格式
    @staticmethod 
    def JsonX(isError,errorTxt="未知"):
        obj={"IsError":isError,"ErrorTxt":errorTxt}
        data = json.dumps(obj)
        return HttpResponse(data, mimetype='application/json')
    
 
    
    
    #静态方法 输出HTML或者txt
    @staticmethod 
    def Html(obj):
        return HttpResponse(obj)
    
    def GetTemplates(self,request):
        count =len(request.path)-1
        return str(request.path)[1:count].replace("/","\\")+".html"
   
    @staticmethod 
    def UpdateModel(request,obj):
        for key in request.POST:
            if hasattr(obj, key): 
                if request.POST.get(key)=="False" or request.POST.get(key)=="True":
                    setattr(obj, key, request.POST.get(key)=="True")
                else:
                    setattr(obj, key, request.POST.get(key))
                
        for key in request.GET:
            if hasattr(obj, key): 
                if request.GET.get(key)=="False" or request.GET.get(key)=="True":
                    setattr(obj, key, request.GET.get(key)=="True")
                else:
                    setattr(obj, key, request.GET.get(key))
                
    #分页 计算
    @staticmethod 
    def paginator(request,list):
        
        after_range_num = 5        #当前页前显示5页
        befor_range_num = 4       #当前页后显示4页
        try: 
            page = int(request.POST.get("page",1))
            if page < 1:
                page = 1
        except ValueError:
            page = 1
        paginator = Paginator(list,20)   #2
        try: 
            page_list = paginator.page(page)
        except(EmptyPage,InvalidPage,PageNotAnInteger):
            page_list = paginator.page(paginator.num_pages)
        
        if page >= after_range_num:
            page_range = paginator.page_range[page-after_range_num:page+befor_range_num]
        else:
            page_range = paginator.page_range[0:int(page)+befor_range_num]
            
        return {'list':page_list,'page_range':page_range}








